package com.diu.domain.strategy.service.rule.chain.impl;

import com.diu.domain.strategy.model.entity.RuleActionEntity;
import com.diu.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.diu.domain.strategy.repository.IStrategyRepository;
import com.diu.domain.strategy.service.rule.chain.AbstractLogicChain;
import com.diu.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import com.diu.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author #diu
 * @description #
 * @create #2025/10/14 11:18
 */
@Slf4j
@Component("rule_blacklist")
public class BackListLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository repository;

    @Override
    public Integer logic(String userId, Long strategyId) {
        log.info("黑名单过滤器触发，userId:{} strategyId:{} ruleModel:{}", userId, strategyId, ruleModel());

        String ruleValue = repository.queryStrategyRuleValue(strategyId, null, ruleModel());
        if(ruleValue != null && !ruleValue.isEmpty()){
            String[] splitRuleValues = ruleValue.split(Constants.COLON);
            Integer awardId = Integer.valueOf(splitRuleValues[0]);
            for(String rule : splitRuleValues[1].split(Constants.SPLIT)){
                if(userId.equals(rule)){
                    log.info("用户命中黑名单，无法参与抽奖，userId:{} strategyId:{}", userId, strategyId);
                    return awardId;
                }
            }
        }

        // 过滤其他责任链
        log.info("抽奖责任链-黑名单放行 userId:{} strategyId:{} ruleModel:{}", userId, strategyId, ruleModel());
        return next().logic(userId, strategyId);
    }

    @Override
    public String ruleModel() {
        return "rule_blacklist";
    }
}
